/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package multi.agent.simulation;

import jade.core.AID;
import jade.core.Agent;
import jade.domain.DFService;
import jade.domain.FIPAAgentManagement.DFAgentDescription;
import jade.domain.FIPAAgentManagement.ServiceDescription;
import jade.domain.FIPAException;
import jade.lang.acl.ACLMessage;
import java.util.Date;
import java.util.UUID;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import multi.agent.simulation.behaviour.MovingRequest;
import multi.agent.simulation.dao.EstadisticaJpaController;
import multi.agent.simulation.dao.NodoJpaController;
import multi.agent.simulation.entity.Estadistica;
import multi.agent.simulation.entity.Nodo;

/**
 *
 * @author Owner
 */
public class MovingAgent extends Agent{
    public String _agentName;
    private String _origin;
    private String _destination;
    private final String _language = "English";
    private final String _ontology = "traffic-ontology";
    private EntityManagerFactory _emf;
    private NodoJpaController _nodeControler;
    private EstadisticaJpaController _statisticControler;
    
    @Override
    protected void setup() {
        
        System.out.println("Setup of  " + _agentName + " in progress.");
        _agentName = getAID().getLocalName();
        
        Object[] args = getArguments();
        
        if(args != null && args.length > 0) {
            //Read the first element. This should be the agent it wants to connect.
            _origin = (String)args[0];
            System.out.println("Origin: " + _origin);
            //Read the second element. This should ber the agent it wants to arrive.
            _destination = (String)args[1];
            System.out.println("Destination: " + _destination);
        }
        else {
            //throws exception?
        }
        
        System.out.println("Registering " + _agentName);
        Register();
        System.out.println("Starting behaviours...");
        InitBehaviours();
        
        //initialize entity
        _emf = Persistence.createEntityManagerFactory("multi-agent-simulationPU");
        _nodeControler = new NodoJpaController(_emf);
        _statisticControler = new EstadisticaJpaController(_emf);
        
        //send statistic
        System.out.println("RegisterStaticts ....");
        SendStatistic(_origin,_destination,"request");

        
        System.out.println("Requesting road...");
        ACLMessage msg = new ACLMessage(ACLMessage.REQUEST); 
        msg.addReceiver(new AID(_origin, false));
        msg.setLanguage(_language);
        msg.setOntology(_ontology);
        BestRoadRequest req = new BestRoadRequest();
        req.DestinationName = _destination;
        req.RequesterName = _agentName;
        req.RequestID = UUID.randomUUID().toString();
        msg.setContent(req.toMessageString());
        this.send(msg); 
        
    }
    
    @Override
    protected void takeDown() {
        try {
            DFService.deregister(MovingAgent.this);
        }
        catch(FIPAException ex) {
            ex.printStackTrace();
        }
    }
    
    private void InitBehaviours() {
        
        MovingRequest behaviour = new MovingRequest(MovingAgent.this, _origin, _destination);
        addBehaviour(behaviour);
    }
    
    private void Register() {
        DFAgentDescription dfd = new DFAgentDescription();
        dfd.setName(getAID());
        ServiceDescription sd = new ServiceDescription();
        sd.setType("moving");
        sd.setName(getName());
        dfd.addServices(sd);
        
        try {
            DFService.register(MovingAgent.this, dfd);
        }
        catch (FIPAException ex) {
            ex.printStackTrace();
        }
    }
    
    public void SendStatistic(String origin, String destiny, String statisticType)
    {
        Nodo originNode = _nodeControler.findNodo(origin);
        Nodo destinyNode = _nodeControler.findNodo(destiny);
        Date now = new Date();
        Estadistica statistic = new Estadistica(1,originNode.getId(),destinyNode.getId(),now,statisticType);
        _statisticControler.create(statistic);
    }
}
